home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / misc / sci / Squid.lha / Squid / squid.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-30  |  13.7 KB  |  373 lines

  1. #include "squid_class.h"
  2. #include "squid_functions.h"
  3. #include <iostream.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <math.h>
  7. #include <graphics/gfxmacros.h>
  8.  
  9. #define BLACK 0x0FFFFFFF
  10. #define WHITE 0xFFFFFFFF
  11. #define RMAX 4
  12. #define RMIN 1
  13. #define XOFFSET 30
  14. #define YOFFSET 50
  15. #define WIDTH 640
  16. #define HEIGHT 480
  17.  
  18. Object *winobj=NULL;
  19.  
  20. struct Window *win=NULL;
  21. struct RastPort winport_noir;
  22. struct RastPort winport_gris;
  23. struct RastPort winport_blanc;
  24. struct RastPort winport_rouge;
  25. struct RastPort winport_vert;
  26. struct RastPort winport_bleu;
  27. struct RastPort *winport=NULL;
  28. struct IntuiMessage *msg=NULL;
  29. struct AreaInfo areainfo;
  30. struct TmpRas tmpras;
  31.  
  32. PLANEPTR bitplane;
  33.  
  34. ULONG noir=0;
  35. ULONG gris=0;
  36. ULONG blanc=0;
  37. ULONG rouge=0;
  38. ULONG vert=0;
  39. ULONG bleu=0;
  40. ULONG color=0;
  41. ULONG input=0L, code=0L;
  42.  
  43. void Window1_CloseWindow_Event(void)
  44. {
  45.     DoMethod(winobj, WM_CLOSE);
  46.     win=NULL;
  47.     winobj=NULL;
  48.     Emperor_QuitProgram();
  49. }
  50.  
  51. void Startup(void)
  52. {
  53.     noir = ObtainBestPen(Screen1->ViewPort.ColorMap, BLACK, BLACK, BLACK, OBP_Precision, PRECISION_EXACT, TAG_DONE);
  54.     blanc = ObtainBestPen(Screen1->ViewPort.ColorMap, WHITE, WHITE, WHITE, OBP_Precision, PRECISION_EXACT, TAG_DONE);
  55.     rouge = ObtainBestPen(Screen1->ViewPort.ColorMap, WHITE, BLACK, BLACK, OBP_Precision, PRECISION_EXACT, TAG_DONE);
  56.     vert = ObtainBestPen(Screen1->ViewPort.ColorMap, BLACK, WHITE, BLACK, OBP_Precision, PRECISION_EXACT, TAG_DONE);
  57.     bleu = ObtainBestPen(Screen1->ViewPort.ColorMap, BLACK, BLACK, WHITE, OBP_Precision, PRECISION_EXACT, TAG_DONE);
  58.  
  59. }
  60.  
  61. void Shutdown(void)
  62. {
  63.     ReleasePen(Screen1->ViewPort.ColorMap, noir);
  64.     ReleasePen(Screen1->ViewPort.ColorMap, blanc);
  65.     ReleasePen(Screen1->ViewPort.ColorMap, rouge);
  66.     ReleasePen(Screen1->ViewPort.ColorMap, vert);
  67.     ReleasePen(Screen1->ViewPort.ColorMap, bleu);
  68. }
  69.  
  70. void Button1_GadgetUp_Event(void)
  71. {
  72.     int s=0, x=0, y=0, cx=0, cy=0, i=0, j=0, r=0, r_diff=0, temp=0, r_temp=0,
  73.     mousex=0, mousey=0, diametre=0, countmax=0, maxx=0, maxy=0, color=0,
  74.     temp_x=0, temp_y=0, cx_min=0, cy_min=0, nbrx=0, nbry=0, target=0, ii=0,
  75.     min=0, iii=0, iiii=0, squid_selected=0, total=0;
  76.     float norm=0.0;
  77.     srand((unsigned) time (NULL));
  78.     total=(WIDTH*HEIGHT*5)/2;
  79.  
  80.     Cell *C = (Cell*) malloc (squid_cell*sizeof(Cell));    
  81.     //float *D = (float*) malloc (sizeof(float));
  82.     //int *ID = (int*) malloc (sizeof(int));
  83.  
  84.     if(C==NULL)
  85.     {
  86.         cout <<"erreur d'allocation\n";
  87.     }
  88.     else
  89.     {
  90.         s = 0;
  91.         diametre = 2*RMAX;
  92.         xsize=(diametre*int(ceil(sqrt(squid_cell))));
  93.         ysize=xsize;
  94.         xsize=xsize+XOFFSET;
  95.         UWORD buffer[(WIDTH*HEIGHT*5)/2];
  96.         Emperor_SetGadgetAttr(String2, inttostring(xsize));
  97.         Emperor_SetGadgetAttr(String3, inttostring(ysize));
  98.         Emperor_SetGadgetAttr(String1, "click on the output window");
  99.  
  100.         winobj = (Object*) WindowObject,
  101.             WA_Left,    100,
  102.             WA_Top,     100,
  103.             WA_Width,   xsize,
  104.             WA_Height,  ysize,
  105.             WA_DragBar, TRUE,
  106.             //WA_Title,   "Sortie graphique",
  107.             WA_PubScreen, Screen1,
  108.             //WA_CloseGadget,  TRUE,
  109.             WA_DepthGadget, TRUE,
  110.             WA_SuperBitMap, TRUE,
  111.             WA_IDCMP,  IDCMP_GADGETDOWN | IDCMP_GADGETUP | IDCMP_GADGETHELP | IDCMP_MENUPICK | IDCMP_MENUHELP | IDCMP_CLOSEWINDOW | IDCMP_ACTIVEWINDOW | IDCMP_INACTIVEWINDOW | IDCMP_RAWKEY | IDCMP_VANILLAKEY | IDCMP_MOUSEBUTTONS | IDCMP_MOUSEMOVE | IDCMP_NEWSIZE | IDCMP_CHANGEWINDOW | IDCMP_SIZEVERIFY | IDCMP_REFRESHWINDOW | IDCMP_INTUITICKS,
  112.             WA_RMBTrap, TRUE,
  113.         EndWindow;
  114.         win = RA_OpenWindow (winobj);
  115.         winport_noir = *win->RPort;
  116.         winport_gris = *win->RPort;
  117.         winport_blanc = *win->RPort;
  118.         winport_rouge = *win->RPort;
  119.         winport_vert = *win->RPort;
  120.         winport_bleu = *win->RPort;
  121.         winport = win->RPort;
  122.         gris = ReadPixel(&winport_gris, 10, 10);
  123.         SetAPen( &winport_gris, gris );
  124.         SetAPen( &winport_noir, noir );
  125.         SetAPen( &winport_blanc, blanc );
  126.         SetAPen( &winport_rouge, rouge );
  127.         SetAPen( &winport_vert, vert );
  128.         SetAPen( &winport_bleu, bleu );
  129.         SetGadgetAttrs(Integer1, Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
  130.         SetGadgetAttrs(Label1, Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
  131.         SetGadgetAttrs(Button1, Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
  132.         SetGadgetAttrs(Button2, Window1, NULL, GA_Disabled, TRUE, TAG_DONE);
  133.  
  134.         InitArea(&areainfo, buffer, HEIGHT);
  135.         bitplane = AllocRaster(WIDTH, HEIGHT);
  136.         if(bitplane==NULL)
  137.         {
  138.             cout <<"erreur du bitplane" <<endl;
  139.         }
  140.         InitTmpRas(&tmpras, bitplane, RASSIZE(WIDTH, HEIGHT));
  141.         winport_noir.AreaInfo=&areainfo;
  142.         winport_noir.TmpRas=&tmpras;
  143.         winport_gris.AreaInfo=&areainfo;
  144.         winport_gris.TmpRas=&tmpras;
  145.         winport_blanc.AreaInfo=&areainfo;
  146.         winport_blanc.TmpRas=&tmpras;
  147.         winport_rouge.AreaInfo=&areainfo;
  148.         winport_rouge.TmpRas=&tmpras;
  149.         winport_vert.AreaInfo=&areainfo;
  150.         winport_vert.TmpRas=&tmpras;
  151.         winport_bleu.AreaInfo=&areainfo;
  152.         winport_bleu.TmpRas=&tmpras;
  153.  
  154.         for(y = 0; y < ysize-(YOFFSET/2); y += diametre)
  155.         {
  156.             for(x = 0; x < xsize-(XOFFSET/2); x += diametre)
  157.             {
  158.                 if(s==squid_cell)
  159.                 {
  160.                     goto stop;
  161.                 }                
  162.                 C[s].set_cx(x);
  163.                 C[s].set_cy(y);
  164.                 r_temp=(rand()%(RMAX+1));
  165.                 C[s].set_r_temp(r_temp);
  166.                 C[s].set_ID(s);
  167.                 color=rand()%5;
  168.                 C[s].set_color(color);
  169.                 switch(color)
  170.                 {
  171.                     case 0:
  172.                         winport=&winport_noir;
  173.                         break;
  174.                     case 1:
  175.                         winport=&winport_blanc;
  176.                         break;
  177.                     case 2:
  178.                         winport=&winport_rouge;
  179.                         break;
  180.                     case 3:
  181.                         winport=&winport_vert;
  182.                         break;
  183.                     case 4:
  184.                         winport=&winport_bleu;
  185.                         break;
  186.                     default:
  187.                         break;
  188.                 }
  189.                 AreaCircle(winport, C[s].get_cx()+(XOFFSET/2), C[s].get_cy()+(YOFFSET/2), RMIN);
  190.                 AreaEnd(winport);
  191.                 s++;
  192.             }
  193.         }
  194.         stop:
  195.  
  196.         //cout <<"allocation de " <<s <<" objets effectuée\n";
  197.         //cout <<"la mémoire utilisée est de " <<s*sizeof(Cell) <<" octets\n";
  198.  
  199.         do
  200.         {
  201.             for(s=0; s<squid_cell; s++)
  202.             {
  203.                 if(C[s].get_update())
  204.                 {
  205.                     Cell *C_selected = (Cell*) malloc (sizeof(Cell));
  206.                     if(C_selected==NULL)
  207.                     {
  208.                         cout <<"erreur d'allocation" <<endl;
  209.                     }
  210.                     else
  211.                     {
  212.                         color=C[s].get_color();
  213.                         switch(color)
  214.                         {
  215.                             case 0:
  216.                                 winport=&winport_noir;
  217.                                 break;
  218.                             case 1:
  219.                                 winport=&winport_blanc;
  220.                                 break;
  221.                             case 2:
  222.                                 winport=&winport_rouge;
  223.                                 break;
  224.                             case 3:
  225.                                 winport=&winport_vert;
  226.                                 break;
  227.                             case 4:
  228.                                 winport=&winport_bleu;
  229.                                 break;
  230.                             default:
  231.                                 break;
  232.                         }
  233.  
  234.                         ii=0;
  235.                         for(i=0; i<squid_cell; i++)
  236.                         {
  237.                             if(C[i].get_color()==color)
  238.                             {
  239.                                 temp_x=C[i].get_cx()-C[s].get_cx();      //obligé d'utiliser un intermédiaire sinon bug
  240.                                 temp_y=C[i].get_cy()-C[s].get_cy();      //idem
  241.                                 norm = sqrt ( (temp_x*temp_x) + (temp_y*temp_y) );
  242.                                 C[i].set_distance(norm);
  243.                                 C_selected[ii]=C[i];
  244.                                 ii++;
  245.                                 C_selected = (Cell*) realloc (C_selected, (ii+1)*sizeof(Cell));
  246.                             }
  247.                         }
  248.                         squid_selected=ii;
  249.  
  250.                         for(i=0; i<squid_selected-1; i++)
  251.                         {
  252.                             Cell temp;
  253.                             if(C_selected[i].get_distance()>C_selected[i+1].get_distance())
  254.                             {
  255.                                 temp=C_selected[i];
  256.                                 C_selected[i]=C_selected[i+1];
  257.                                 C_selected[i+1]=temp;
  258.                                 for(ii=i; ii>=1; ii--)
  259.                                 {
  260.                                     if(C_selected[ii-1].get_distance()<C_selected[ii].get_distance())
  261.                                     {
  262.                                         goto escape;
  263.                                     }
  264.                                     else
  265.                                     {
  266.                                         temp=C_selected[ii-1];
  267.                                         C_selected[ii-1]=C_selected[ii];
  268.                                         C_selected[ii]=temp;
  269.                                     }
  270.                                 }
  271.                                 escape:
  272.                             }
  273.                         }
  274.  
  275.                         for(r=RMIN; r<=RMAX; r++)
  276.                         {
  277.                             for(i=0; i<squid_selected; i++)
  278.                             {
  279.                                 AreaCircle(&winport_gris, C_selected[i].get_cx()+(XOFFSET/2), C_selected[i].get_cy()+(YOFFSET/2), r-1);
  280.                                 AreaEnd(&winport_gris);
  281.                                 if(r!=RMAX)
  282.                                 {
  283.                                     AreaCircle(winport, C_selected[i].get_cx()+(XOFFSET/2), C_selected[i].get_cy()+(YOFFSET/2), r);
  284.                                     AreaEnd(winport);
  285.                                 }
  286.                                 else
  287.                                 {
  288.                                     goto arriere;
  289.                                 }
  290.                             }
  291.                         }
  292.                         arriere:
  293.                         for(r=RMAX-1; r>=RMIN; r--)
  294.                         {
  295.                             for(i=0; i<squid_selected; i++)
  296.                             {
  297.                                 AreaCircle(&winport_gris, C_selected[i].get_cx()+(XOFFSET/2), C_selected[i].get_cy()+(YOFFSET/2), r+1);
  298.                                 AreaEnd(&winport_gris);
  299.                                 AreaCircle(winport, C_selected[i].get_cx()+(XOFFSET/2), C_selected[i].get_cy()+(YOFFSET/2), r);
  300.                                 AreaEnd(winport);
  301.                             }
  302.                         }
  303.                         C[s].set_update(FALSE);
  304.                     }
  305.                     free(C_selected);
  306.                 }
  307.             }
  308.             while(msg = (struct IntuiMessage*) GetMsg(win->UserPort))
  309.             {
  310.                 switch(msg->Class)
  311.                 {
  312.                     case IDCMP_MOUSEBUTTONS:
  313.                         switch (msg->Code)
  314.                         {
  315.                             case SELECTDOWN:
  316.                                 mousex=win->MouseX-(XOFFSET/2);
  317.                                 mousey=win->MouseY-(YOFFSET/2);
  318.                                 cx_min=1000;
  319.                                 cy_min=1000;
  320.                                 for(s=0; s<squid_cell; s++)
  321.                                 {
  322.                                     temp_x = (mousex-C[s].get_cx());
  323.                                     temp_y = (mousey-C[s].get_cy());
  324.                                     temp_x = (temp_x < 0 ? -temp_x : temp_x);
  325.                                     temp_y = (temp_y < 0 ? -temp_y : temp_y);
  326.                                     if((temp_x<=cx_min) && (temp_y<=cy_min))
  327.                                     {
  328.                                         cx_min=temp_x;
  329.                                         cy_min=temp_y;
  330.                                         target=s
  331.                                     }
  332.                                 }
  333.                                 C[target].set_update(TRUE);
  334.                                 break;
  335.                             case SELECTUP:
  336.                                 break;
  337.                             default:
  338.                                 break;
  339.                         }
  340.                     default:
  341.                         break;
  342.                 }
  343.             }
  344.         }
  345.         while(!IO_boucle());
  346.         interruption:
  347.         DoMethod(winobj, WM_CLOSE);
  348.         win=NULL;
  349.         winobj=NULL;        
  350.         FreeRaster(bitplane, WIDTH, HEIGHT);
  351.         free(C);
  352.         SetGadgetAttrs(Integer1, Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
  353.         SetGadgetAttrs(Label1, Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
  354.         SetGadgetAttrs(Button1, Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
  355.         SetGadgetAttrs(Button2, Window1, NULL, GA_Disabled, FALSE, TAG_DONE);
  356.     }
  357. }
  358.  
  359. void Integer1_GadgetUp_Event(void)
  360. {
  361.     squid_cell = atoi(Emperor_GetGadgetAttr(Integer1));
  362. }
  363.  
  364. void Button2_GadgetUp_Event(void)
  365. {
  366.     squid_cell = 10;
  367.     SetGadgetAttrs(Integer1, Window1, NULL, INTEGER_Number, 10, TAG_DONE);
  368. }
  369.  
  370. void Button3_GadgetUp_Event(void)
  371. {
  372. }
  373.